package lab03.test09;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test09 {
    public static void main(String[] args) {
        //txt文件内容->List
        //单步调试
        //断点

        List<Book> books=new ArrayList<Book>();

        String path="D:\\java实验\\book.txt";
        String path1="D:\\java实验\\book1.txt";
        BufferedReader reader=null;
        BufferedWriter writer=null;


       /* 错误
       1.编译错误   ——alt + enter
       2.运行时错误——调试

        */
        try {
            try {
                reader = new BufferedReader(new FileReader(path));

                String line =null;
                while ((line=reader.readLine())!=null ){
                    System.out.println(line);
                    Book  book=new Book();
                    String[] tokens=line.split("\t");
                    book.number=Integer.parseInt(tokens[0]);
                    book.name=tokens[1];
                    book.version=tokens[2];
                    book.price=Double.parseDouble(tokens[3]);
                    book.sales=Double.parseDouble(tokens[4]);

                    books.add(book);
                }
                int i=1;
                for (Book b:books){
                    if (i==3){
                        b.price=b.price+20;
                    }
                    if (i==4){
                        b.price=b.price+30;
                    }
                    i++;
                }
                writer=new BufferedWriter(new FileWriter(path1));
                for (Book  b:books){
                    writer.write(b.name+" "+b.number+" "+b.version+" "+b.price+" "+b.sales);
                    writer.newLine();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        finally {
            try {
                writer.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
